<template>
  <div class="app-container">
    <div style="width:100%;height:30px;display: flex;
 justify-content: flex-end;
">
      <el-button
          size="mini"
          type="primary" round
          @click="goToBack()"
      >返回上一页
      </el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!--<el-form-item label="所属保险id" prop="insureId">-->
      <!--  <el-input-->
      <!--      v-model="queryParams.insureId"-->
      <!--      placeholder="请输入所属保险id"-->
      <!--      clearable-->
      <!--      @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属理赔id" prop="settleId">-->
      <!--  <el-input-->
      <!--      v-model="queryParams.settleId"-->
      <!--      placeholder="请输入所属理赔id"-->
      <!--      clearable-->
      <!--      @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属赔案id" prop="caseId">-->
      <!--  <el-input-->
      <!--      v-model="queryParams.caseId"-->
      <!--      placeholder="请输入所属赔案id"-->
      <!--      clearable-->
      <!--      @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <el-form-item label="收款人名称" prop="recipient">
        <el-input
            v-model="queryParams.recipient"
            placeholder="请输入收款人名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-input
            v-model="queryParams.payType"
            placeholder="请输入支付方式"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="openingBank">
        <el-input
            v-model="queryParams.openingBank"
            placeholder="请输入开户行"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账户" prop="accountNumber">
        <el-input
            v-model="queryParams.accountNumber"
            placeholder="请输入银行账户"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="支付时间">-->
      <!--  <el-date-picker-->
      <!--      v-model="daterangePayTime"-->
      <!--      style="width: 240px"-->
      <!--      value-format="yyyy-MM-dd"-->
      <!--      type="daterange"-->
      <!--      range-separator="-"-->
      <!--      start-placeholder="开始日期"-->
      <!--      end-placeholder="结束日期"-->
      <!--  ></el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['insurance:pay:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['insurance:pay:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:pay:remove']"
        >删除
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--      type="warning"-->
      <!--      plain-->
      <!--      icon="el-icon-download"-->
      <!--      size="mini"-->
      <!--      @click="handleExport"-->
      <!--      v-hasPermi="['insurance:pay:export']"-->
      <!--  >导出-->
      <!--  </el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="" align="center" prop="id"/>-->
      <!--<el-table-column label="所属保险id" align="center" prop="insureId"/>-->
      <!--<el-table-column label="所属理赔id" align="center" prop="settleId"/>-->
      <!--<el-table-column label="所属赔案id" align="center" prop="caseId"/>-->
      <el-table-column label="收款人名称" align="center" prop="recipient"/>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settle_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="开户行" align="center" prop="openingBank"/>
      <el-table-column label="银行账户" align="center" prop="accountNumber"/>
      <el-table-column label="支付金额(元)" align="center" prop="payAmount"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="附件" align="center" prop="attachment"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:pay:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:pay:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改保险理赔赔案支付信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="收款人名称" prop="recipient">
          <el-input v-model="form.recipient" placeholder="请输入收款人名称"/>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <!--<el-input v-model="form.payType" placeholder="请输入支付方式"/>-->
          <el-select v-model="form.payType" placeholder="请选择任务状态" clearable>
            <el-option
                v-for="dict in dict.type.settle_pay_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开户行" prop="openingBank">
          <el-input v-model="form.openingBank" placeholder="请输入开户行"/>
        </el-form-item>
        <el-form-item label="银行账户" prop="accountNumber">
          <el-input v-model="form.accountNumber" placeholder="请输入银行账户"/>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入支付金额">
            <template slot="append">
              <el-button slot="append">元</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
                          v-model="form.payTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="附件" prop="attachment">
          <file-upload v-model="form.attachment" :fileType="['pdf']" :fileSize="99999" :limit="99999"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPay, getPay, delPay, addPay, updatePay} from "@/api/insurance/pay";
import {isEmpty, isValidAmount} from "@/utils/validate";

export default {
  dicts: ['settle_pay_type'],
  name: "Pay",
  data() {
    const checkPayAmount = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }
      if (!isValidAmount(value)) {
        callback(new Error('请输入正确的金额'));
        return;
      }
      callback();
    }
    return {
      settleId: undefined,
      insureId: undefined,
      caseId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险理赔赔案支付信息表格数据
      payList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 时间范围
      daterangePayTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insureId: null,
        settleId: null,
        caseId: null,
        recipient: null,
        payType: null,
        openingBank: null,
        accountNumber: null,
        payAmount: null,
        payTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        payAmount: [
          {validator: checkPayAmount, trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.settleId = this.$route.query.settleId
    this.insureId = this.$route.query.insureId
    this.caseId = this.$route.query.caseId
    if (isEmpty(this.settleId) || isEmpty(this.insureId) || isEmpty(this.caseId)) {
      this.$modal.msgWarning('请重新进入页面')
      return
    }
    this.getList();
  },
  methods: {
    goToBack() {
      this.$router.push({path: "/insurance/case-list/case/" + this.insureId, query: {'settleId': this.settleId}});
    },
    /** 查询保险理赔赔案支付信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.settleId = this.settleId
      this.queryParams.insureId = this.insureId
      this.queryParams.caseId = this.caseId
      this.queryParams.params = {};
      if (null != this.daterangePayTime && '' != this.daterangePayTime) {
        this.queryParams.params["beginPayTime"] = this.daterangePayTime[0];
        this.queryParams.params["endPayTime"] = this.daterangePayTime[1];
      }
      listPay(this.queryParams).then(response => {
        this.payList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insureId: null,
        settleId: null,
        caseId: null,
        recipient: null,
        payType: null,
        openingBank: null,
        accountNumber: null,
        payAmount: null,
        payTime: null,
        attachment: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePayTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险理赔赔案支付信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保险理赔赔案支付信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.settleId = this.settleId
          this.form.insureId = this.insureId
          this.form.caseId = this.caseId
          if (this.form.id != null) {
            updatePay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的保险理赔赔案支付信息？').then(function () {
        return delPay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/pay/export', {
        ...this.queryParams
      }, `pay_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
